using System;
using System.IO;
using System.Text;
using System.Reflection;
using System.Threading;

//using IronPython;
//using IronPython.Hosting;
//using Microsoft.Scripting;
//using Microsoft.Scripting.Hosting;


using Calibur;
using EggWorld.Protocol;

namespace EggWorld {
	public class MyLogger {
		static MyLogger() {
			Console.WriteLine("MyLogger's static constructor.");
		}

		public static void Log(string msg) {
			Console.WriteLine("Log: " + msg);
		}
	}

	public class Bean {
		int id;
		string name;

		public Bean(int id, string name) {
			this.id = id;
			this.name = name;
		}

		public int Id {
			get { return Id; }
			set { id = value; }
		}

		public string Name {
			get { return name; }
			set { name = value; }
		}
	}

	public class Demo {
		public static void Main(string[] args) {
			testMsgBuf();
		}

//		public static void testPython() {
//			ScriptEngine _engine;
//			ScriptRuntime _runtime;
//			CompiledCode _code;
//			ScriptScope _scope;
//
//			string source = @"d:\tmp\test.py";
//			_engine = Python.CreateEngine();
//			_runtime = _engine.Runtime;
//			_scope = _engine.CreateScope();
//			
//			ScriptSource _script = _engine.CreateScriptSourceFromFile(source, Encoding.UTF8, SourceCodeKind.File);
//			_code = _script.Compile(); // error when running this line
//
//			Bean bean = new Bean(1, "benjamin");
//			_scope.SetVariable("bean", bean);
//			_code.Execute(_scope);
//			Console.WriteLine("Return bean's name=" + bean.Name);
//		}
		
		private static void testReflect() {
			UnityEngine.Debug.Log("hello");
			Type type = Type.GetType("UnityEngine.Debug");
			Object obj = Activator.CreateInstance(type);
			type.InvokeMember("Log", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, new object[] {"hello Logger"});
		}
		
		private static void testSession() {
			Session session = new Session(new ObjectFactory());
			
			int retcode = session.Open("127.0.0.1", 10000);
			Console.WriteLine("retcode={0}", retcode);
			
			// Login
			Login login = new Login(1000);
			login.Passport = "john";
			login.Password = "123456";
			MessageBean resp;
			
			session.SendMessageBean(login);
			resp = Recv(session, 5);
			if (resp == null) {
				Console.WriteLine("null");
			} else {
				Console.WriteLine(resp.ToString());
			}
			
			session.SendMessageBean(login);
			resp = Recv(session, 5);
			if (resp == null) {
				Console.WriteLine("null");
			} else {
				Console.WriteLine(resp.ToString());
			}
			
			
			session.Close();
			session.Open("127.0.0.1", 10000);
			session.SendMessageBean(login);
			resp = Recv(session, 5);
			if (resp == null) {
				Console.WriteLine("null");
			} else {
				Console.WriteLine(resp.ToString());
			}
		}
		
		private static MessageBean Recv(Session session, int timeout) {
			for (int i = 0; i < timeout; ++i) {
				MessageBean mbean = session.RecvMessageBean();
				if (mbean != null) {
					return mbean;
				} else {
					Thread.Sleep(1000);
				}
			}
			
			return null;
		}
		
		private static void testMsgBuf() {
			FileStream fs = new FileStream("/tmp/1", FileMode.Open);
			MessageBeanFactory factory = new ObjectFactory();
			MessageBean mbean = factory.DeserializeMessageBean(fs, 2000);
			Console.WriteLine(mbean.ToString());
			fs.Close();
		}
		
		private static void testByte() {
			int s = -2;
			byte b = (byte) s;
			int a = (int) b;
			Console.WriteLine("byte=" + a);
		}
		
		private static void testDebug() {
#if DEBUG
			Console.WriteLine("Debug");
#endif
		}
	}
}